const { Model, DataTypes } = require('sequelize');
const sequelize = require('../db/db');
const User = require('./User');

class Feedback extends Model { }

Feedback.init(
  {
    id: {
      type: DataTypes.CHAR(18),
      primaryKey: true,
      allowNull: false,
      comment: '反馈ID'
    },
    create_id: {
      type: DataTypes.CHAR(18),
      allowNull: false,
      comment: '创建人ID'
    },
    feedback_type: {
      type: DataTypes.TINYINT,
      allowNull: false,
      comment: '反馈类型(1=产品建议,2=功能问题,3=内容问题,4=其他)'
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false,
      comment: '反馈内容'
    },
    contact_info: {
      type: DataTypes.STRING(100),
      comment: '联系方式'
    },
    status: {
      type: DataTypes.TINYINT,
      defaultValue: 1,
      allowNull: false,
      comment: '处理状态(1=待处理,2=已解决,3=不予处理)'
    },
    solve_id: {
      type: DataTypes.CHAR(18),
      comment: '处理人ID'
    },
    created_time: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      comment: '创建时间'
    },
    updated_time: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      comment: '更新时间'
    },
    remark: {
      type: DataTypes.TEXT,
      comment: '备注',
    }
  },
  {
    sequelize,
    modelName: 'Feedback',
    tableName: 'feedback',
    timestamps: false,
    comment: '意见反馈表'
  }
);

// 添加与 User 模型的关联
Feedback.belongsTo(User, {
  foreignKey: 'create_id',
  targetKey: 'user_id',
  as: 'creator'
});

Feedback.belongsTo(User, {
  foreignKey: 'solve_id',
  targetKey: 'user_id',
  as: 'solver'
});

module.exports = Feedback;